package com.tg.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tg.app.R;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGApplicationBase;

public class ACCameraShowErrorView extends RelativeLayout {
    private TextView mTextView;
    private int mTextColor;
    private int mTextTipColor;
    private ImageView mImageView;
    private boolean mShowImage;
    private TextView mTextErrorTip;
    private Button mButton;
    private ACPlayBackView.OnPlaybackListener onPlaybackListener;
    private Drawable mDrawableImage;
    public void setErrorInfo(String text, String textTip,String buttonText){
        this.setText(text);
        this.setButtonText(buttonText);
        this.setTextTip(textTip);
    }

    public void setCarErrorInfo(){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.playback_car_service_disable),
        TGApplicationBase.getApplication().getString(R.string.playback_car_service_disable_tips),
                TGApplicationBase.getApplication().getString(R.string.open_now));
    }

    public void setCarPrimaryErrorInfo(){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.playback_car_service_primary1), null,
                TGApplicationBase.getApplication().getString(R.string.upgrade_now));
    }

    public void setWifiErrorInfo(){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.playback_none_cloud),
                TGApplicationBase.getApplication().getString(R.string.no_sdcard_restart_camera),
                TGApplicationBase.getApplication().getString(R.string.open_now));
    }

    public void setPassiveDeviceErrorInfo(){
        mImageView.setImageResource(R.drawable.ic_tange_global_vip_live_page);
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.servic_vip_no_find),
                TGApplicationBase.getApplication().getString(R.string.upgrade_vip_service),
                TGApplicationBase.getApplication().getString(R.string.open_now));
    }



    public void setLampErrorInfo(boolean service){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.learning_services_notpurchased),
                TGApplicationBase.getApplication().getString(R.string.lamp_study_service_tip),
                TGApplicationBase.getApplication().getString(service? R.string.go_upgrade : R.string.go_open));
    }

    public void setSimCloudErrorInfo(){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.playback_sim_none_cloud),
                TGApplicationBase.getApplication().getString(R.string.playback_sim_none_cloud_disable_tips),
                TGApplicationBase.getApplication().getString(R.string.open_now));
    }
    public void setSimCloudExpiredInfo(){
        this.setErrorInfo(TGApplicationBase.getApplication().getString(R.string.playback_sim_expired_cloud),
                TGApplicationBase.getApplication().getString(R.string.playback_sim_none_expired_disable_tips),
                TGApplicationBase.getApplication().getString(R.string.upgrade_now));
    }

    public void setSimExpiredInfo(){
        this.setText(TGApplicationBase.getApplication().getString(R.string.sim_service_expired));
        this.mButton.setVisibility(GONE);
        this.mImageView.setVisibility(GONE);
        this.mTextErrorTip.setVisibility(GONE);
    }

    public void setSdCardNone(){
        mButton.setVisibility(GONE);
        mImageView.setImageResource(R.drawable.ic_global_tange_sd);
        setText(TGApplicationBase.getApplication().getString(R.string.playback_none_sdcard));
        setTextTip(TGApplicationBase.getApplication().getString(R.string.playback_none_sdcard_tips));
    }
    public void setSdCardVideoNone(){
        mButton.setVisibility(GONE);
        mImageView.setImageResource(R.mipmap.image_camera_show_error_sdcard_video);
        setText(TGApplicationBase.getApplication().getString(R.string.playback_none_sdcard_video));
        mTextErrorTip.setVisibility(GONE);

    }
    public void setButOnClickListener(OnClickListener butOnClickListener) {
        mButOnClickListener = butOnClickListener;
    }

    private OnClickListener mButOnClickListener;
    public ACCameraShowErrorView(Context context) {
        super(context);
        initContentView(context);
    }

    public ACCameraShowErrorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public void setOnPlaybackListener(ACPlayBackView.OnPlaybackListener onClickListener) {
        onPlaybackListener = onClickListener;
    }

    public ACCameraShowErrorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }

    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ACCameraShowErrorView);
        mDrawableImage = typedArray.getDrawable(R.styleable.ACCameraShowErrorView_errorImage);
        mShowImage = typedArray.getBoolean(R.styleable.ACCameraShowErrorView_showErrorImage, true);
        mTextColor = typedArray.getColor(R.styleable.ACCameraShowErrorView_errorTextColor, Color.parseColor("#171931"));
        mTextTipColor = typedArray.getColor(R.styleable.ACCameraShowErrorView_errorTextTipColor, Color.parseColor("#171931"));
        typedArray.recycle();
    }

    public void setText(String text){
        if (mTextView != null){
            mTextView.setText(text);
        }
    }

    public String getText(){
        if (mTextView != null){
            return mTextView.getText().toString();
        }
        return null;
    }


    public void setButtonText(String text){
        mButton.setText(text);
    }

    public Button getButton(){
        return mButton;
    }
    public void hideErrorImage(){
        if (mImageView != null){
            mImageView.setVisibility(GONE);
        }
    }

    public void setTextTip(String textTip){
        if (mTextErrorTip != null){
            mTextErrorTip.setText(textTip);
            mTextErrorTip.setVisibility(VISIBLE);
        }
    }

    public void setEmptyDataText(String text) {
        hideErrorImage();
        setText(text);
        mTextErrorTip.setVisibility(GONE);
        mButton.setVisibility(GONE);
    }

    private void initContentView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.layout_camera_show_error_view, this, true);
        mTextView = rootView.findViewById(R.id.cloud_service_disable);
        mImageView = rootView.findViewById(R.id.image_error);
        mTextErrorTip = rootView.findViewById(R.id.service_disable_tips);
        mButton = rootView.findViewById(R.id.btn_playback_view_buy);
        if (!mShowImage){
            hideErrorImage();
        }else if (mDrawableImage != null){
            mImageView.setImageDrawable(mDrawableImage);
        }else{
            mImageView.setImageResource(R.drawable.ic_global_tange_study);
        }
        mTextView.setTextColor(mTextColor);
        mTextErrorTip.setTextColor(mTextTipColor);
        mButton.setOnClickListener(v -> {
            if (onPlaybackListener != null){
                onPlaybackListener.onBuyClick();
            }else if (mButOnClickListener != null){
                mButOnClickListener.onClick(v);
            }
        });
    }
}
